<?php
/*
 * 权限MVC生成工具
 * @since 1.0 2014-9-3 by sutroon
 * @remark 虽然有点简陋，但是还是佩服俺自己!
 */
require 'Inc/incDbHead.php';
require 'Inc/incDefinedFileFunc.php';
require 'Data/Cur_PermissionData.php';
require 'Data/Cur_DefinedFile.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <title>SoPermissionSqlGererate</title>
        <meta charset="UTF-8">
        <style type='text/css'>
            body { font-size:12px;}
            table{width:100%; border: solid 1px #CCC; border-left:none; border-bottom: none;}
            th:first-child{width:30px;}
            .th-field {width:500px;}
            th, td { padding: 5px; border:solid 1px #CCC; border-right: none; border-top: none;}
            tr.tr-alt { background-color: #efefef;}
            a { text-decoration: none;}
            fieldset{ margin-bottom:10px; width:960px;}
            fieldset textarea { width:100%; height:600px;}
        </style>
        <script type="text/javascript" src="/Public/Script/jquery-1.11.1.min.js"></script>
    </head>
    <body>
        <?php
        // 全局菜单
        include 'Inc/incMenu.php';

        // 生成页面文件
        make_page($DATABASE_SET, $permission_data);

        /**
         * 格式化数组
         * 用于make_page函数中调用,主要处理数组中的url字段,如?s=User/admin_edit/id/{key}.html,
         *      则格式化为array($model1=>array($action1=>array('name'=>$name1,'autocoding'=>'modelname:actionname:mvc.actionfunc:table:fields')...)...)
         * @param array $arr
         * @param array $arrfmt
         * @return array
         * @since 1.0 2014-7-31 by sutroon
         */
        function format_array($arr, &$arrfmt) {
            $url;
            foreach ($arr as $a) {
                $url = $a['url'];
                if ($url !== '' && strpos($url, '?s=Index/index/') === false) {
                    $url = substr($url, 3);
                    $arrurl = explode('/', $url);
                    $model = $arrurl[0];
                    $action = isset($arrurl[1]) ? $arrurl[1] : '';
                    if (strpos($action, '.') !== false) {
                        $action = substr($action, 0, strpos($action, '.'));
                    }
                    $arrfmt[$model][$action] = array('name' => $a['name'], 'autocoding' => $model . ':' . $action . ':' . (isset($a['autocoding']) ? $a['autocoding'] : ''));
                }
                if (array_key_exists('sub', $a)) {
                    format_array($a['sub'], $arrfmt);
                }
            }
            return $arrfmt;
        }

        /**
         * 生成Action页面和视图页面文件<br />
         * Action页面命名格式：{Action}Action.class.php<br />
         * 视图页面文件放在./Temp目录里面
         * @param type $arr
         * @since 
         *      2.0 2014-7-31 by sutroon 重构
         *      1.0 2014-7-17 by sutroon 创建
         * @example make_page($arr);
         */
        function make_page($DATABASE_SET, $arr) {
            $date = date('Y-m-d H:i:s');
            $arrfmt;
            if (!is_dir('./Temp/')) {
                mkdir('./Temp/');
            }
            format_array($arr, $arrfmt);
            //var_dump($arrfmt);
            //die('-');
            foreach ($arrfmt as $control => $arr_func) {
                $content = '<?php' . PHP_EOL . '/**' . PHP_EOL . ' * ' . $control . '控制器' . PHP_EOL . ' * @since 1.0 ' . $date . ' by sutroon' . PHP_EOL . ' */' . PHP_EOL . 'class ' . $control . 'Action extends AdminPageAction {' . PHP_EOL;
                foreach ($arr_func as $method => $arr_info) {
                    if (!$method) {
                        $method = 'index';
                    }
                    $autocoding = $arr_info['autocoding'] ? explode(':', $arr_info['autocoding']) : null; // User:admin_list:vc.list:tuser_member:id,siteID,userName,blance,maxTask,outFeeRate,inFeeRate,allowOverdraftAmount,state,createdTime
                    $mvc = '';
                    $func = '';
                    $table = '';
                    $fields = '';
                    if (!is_null($autocoding) && count($autocoding) > 3) {
                        $table = $autocoding[3];
                        $fields = $autocoding[4];
                        $mvc = substr($autocoding[2], 0, strpos($autocoding[2], '.'));
                        $func = substr($autocoding[2], strpos($autocoding[2], '.')+1);
                    }
                    $content .= PHP_EOL . '     /**' . PHP_EOL . '     * 显示' . $arr_info['name'] . PHP_EOL . '     * @since 1.0 ' . $date . ' by sutroon' . PHP_EOL . '     */' . PHP_EOL . '    public function ' . $method . '() {' . PHP_EOL . '        ';
                    if (is_null($autocoding)) {
                        $content.='$this->display();';
                    } else {
                        $content .= definedfile_to_mvccontrol_code($DATABASE_SET, $table, $fields, $func);
                    }
                    $content .= PHP_EOL . '    }' . PHP_EOL;
                    // write method file
                    if (is_null($autocoding) || (strpos($mvc, 'v') !== false)) {
                        if (!is_dir('./Temp/' . $control . '/')) {
                            mkdir('./Temp/' . $control . '/');
                        }
                        $page_content = '';
                        if ($mvc) {
                            switch ($func) {
                                case 'edit':
                                    $page_content = definedfile_to_formui_code($DATABASE_SET, $table, $fields);
                                    break;
                                case 'list':
                                    $page_content = definedfile_to_tableui_code($DATABASE_SET, $table, $fields);
                                    break;
                            }
                        }
                        file_put_contents('./Temp/' . $control . '/' . $method . '.html', '<!--' . PHP_EOL . $arr_info['name'] . PHP_EOL . '@since 1.0 ' . $date . ' by sutroon' . PHP_EOL . '-->' . PHP_EOL . $page_content);
                    }
                }
                $content.='}';
                $path = './Temp/' . $control . 'Action.class.php';
                file_put_contents($path, $content);
            }
            $path = $_SERVER['SCRIPT_NAME'];
            $path = substr($path, 0, strrpos($path, '/'));
            $path = str_replace('/', '\\', $path) . '\\Temp';
            echo '<div>success! <a href="SoProjectManager.php?path=' . $_SERVER['DOCUMENT_ROOT'] . $path . '" target="_blank">[Temp]</a></div>';
        }

        // 释放资源
        unset($db);
        ?>
    </body>
    <script type="text/javascript">
        // fieldset标题点击收起或展开 2014-8-22 by sutroon
        $('legend').attr('title', '点击展开或收起').click(function() {
            $(this).next().toggle();
        })
    </script>
</html>


